****************************************************************************
**		Authors: 	Or Tuttnauer & Liran Harsgor
**		Purpose: 	Produce Figure A3 in Kedar, Harsgor & Tuttnauer (JOP)  
**		input:		KHT_countrylevel.dta
*****************************************************************************

log using "figure a3.log", replace

set more off
use "KHT_countrylevel.dta", clear

*interaction terms
gen x1x2	=enpv * lnmeddm  	/* interaction lnmeddm and enpv*/
gen x1x3	=enpv 			* sr
gen x2x3	=		lnmeddm * sr
gen x1x2x3	=enpv * lnmeddm * sr

* Generate x2,x3 with all values of modifier 
	set obs 902000
	generate magnitude_f = (int((_n-1)/200))/1100
	gen sr_f = (mod(_n-1,200))/36

** baseline model **	
reg enps enpv lnmeddm x1x2 smd mmm fused upper

* Save coefficients and V-CV matrix *	
	matrix b=e(b)
	matrix V=e(V)
	scalar b1=b[1,1]  		/* first coefficient (enpv) */
	scalar b3=b[1,3]		/* first interaction's coefficient */
	scalar varb1=V[1,1]		/* variance of b1 */
	scalar varb3=V[3,3]		/* variance of b4 */
	scalar covb1b3=V[1,3]	/* cov(b1 b4) */

* Marginal effect of enpv on enps
gen conbx_fict= b1+b3*magnitude_f
	
** Scatter plot **
recode conbx_fict (.00/.50=0) (.5001/.6=1) ///
		(.6001/.70=2)(.7001/.80=3)(.8001/.90=4)(.9001/1=5)(1.001/1.5=6), gen(conbx_cat)
		
	twoway  scatter sr_f magnitude_f if conbx_c==0 & sr_f>=1, mcolor(gs14) ///
			msize(vsmall) msymbol(D) || ///
			scatter sr_f magnitude_f if conbx_c==1 & sr_f>=1, mcolor(gs13) ///
			msize(vsmall) msymbol(D) || ///
			scatter	sr_f magnitude_f if conbx_c==2 & sr_f>=1, mcolor(gs12) ///
			msize(vsmall) msymbol(D) || ///
			scatter	sr_f magnitude_f if conbx_c==3 & sr_f>=1, mcolor(gs11) ///
			msize(vsmall) msymbol(D) || ///
			scatter	sr_f magnitude_f if conbx_c==4 & sr_f>=1, mcolor(gs9) ///
			msize(vsmall) msymbol(D) || ///
			scatter	sr_f magnitude_f if conbx_c==5 & sr_f>=1, mcolor(gs8) ///
			msize(vsmall) msymbol(D) || ///
			scatter	sr_f magnitude_f if conbx_c==6 & sr_f>=1, mcolor(gs6) ///
			msize(vsmall) msymbol(D) || ///
			scatter sr lnmeddm if dpr==1, msymbol(X) ///
			mcolor(black) mlabel(cyear) ///
			mlabcolor(black) mlabsize(vsmall) xtitle("Median magnitude (logged)") ///
			ytitle("Seat ratio") /* legend(order(1 2 3 4 5 6 7)	label(1 "0.4-0.5") ///
			label (2 "0.5-0.6") label(3 "0.6-0.7") label(4 "0.7-0.8") ///
			label(5 "0.8-0.9") label(6 "0.9-1.0") label(7 "1.0-1.12") cols(4) ///
			size(*.65) ) */ legend(off) ylabel(1 2 3 4 5)
	
graph save "effect_scatter_basesr.gph", replace
	
drop conbx_fict conbx_cat

*** ENPV and lnmeddm and sr 
reg enps enpv lnmeddm sr x1x2 x1x3 x2x3 x1x2x3 smd mmm fused upper

* Save coefficients and V-CV matrix *	
	matrix b=e(b)
	matrix V=e(V)
	scalar b1=b[1,1]  		/* first coefficient (enpv) */
	scalar b4=b[1,4]		/* first interaction's coefficient */
	scalar b5=b[1,5]		/* second interaction's coefficient */
	scalar b7=b[1,7]		/* triple interaction's coefficient */
	scalar varb1=V[1,1]		/* variance of b1 */
	scalar varb4=V[4,4]		/* variance of b4 */
	scalar varb5=V[5,5]		/* variance of b5 */
	scalar varb7=V[7,7]		/* variance of b7 */
	scalar covb1b4=V[1,4]	/* cov(b1 b4) */
	scalar covb1b5=V[1,5]	/* cov(b1 b5) */
	scalar covb4b5=V[4,5]	/* cov(b4 b5) */
	scalar covb1b7=V[1,7]	/* cov(b1 b7) */
	scalar covb4b7=V[4,7]	/* cov(b4 b7) */
	scalar covb5b7=V[5,7]	/* cov(b5 b7) */
	
* Marginal effect of enpv on enps
	gen conbx_fict= b1+b4*magnitude_f+b5*sr_f+b7*magnitude_f*sr_f

** Scatter plot **

	recode conbx_fict (.00/.50=0) (.5001/.6=1) ///
		(.6001/.70=2)(.7001/.80=3)(.8001/.90=4)(.9001/1=5)(1.001/1.5=6), gen(conbx_cat)
		
	twoway  scatter sr_f magnitude_f if conbx_c==0 & sr_f>=1, mcolor(gs14) ///
	msize(vsmall) msymbol(D) || ///
			scatter sr_f magnitude_f if conbx_c==1 & sr_f>=1, mcolor(gs13) ///
			msize(vsmall) msymbol(D) || ///
			scatter	sr_f magnitude_f if conbx_c==2 & sr_f>=1, mcolor(gs12) ///
			msize(vsmall) msymbol(D) || ///
			scatter	sr_f magnitude_f if conbx_c==3 & sr_f>=1, mcolor(gs11) ///
			msize(vsmall) msymbol(D) || ///
			scatter	sr_f magnitude_f if conbx_c==4 & sr_f>=1, mcolor(gs9) ///
			msize(vsmall) msymbol(D) || ///
			scatter	sr_f magnitude_f if conbx_c==5 & sr_f>=1, mcolor(gs8) ///
			msize(vsmall) msymbol(D) || ///
			scatter	sr_f magnitude_f if conbx_c==6 & sr_f>=1, mcolor(gs6) ///
			msize(vsmall) msymbol(D) || ///
			scatter sr lnmeddm if dpr==1, msymbol(X) ///
			mcolor(black) mlabel(cyear) mlabcolor(black) mlabsize(vsmall) ///
			xtitle("Median magnitude (logged)") ///
			ylabel(1 2 3 4 5) legend(off)
graph save "triple interaction 3d.gph", replace

graph combine effect_scatter_basesr.gph "triple interaction 3d.gph", commonscheme ///
ysize(10) xsize(19.5)scheme(s1mono) xcommon
graph save "figure a3.gph", replace

log close
